// pages/goodsDetail/goodsDetail.js

const util = require('../../utils/util')
const { GoodsDetail, GoodsRelated,getCartData,putGoodsIntoCart } = require('../../config/api')

Page({

  data: {
    goodsId: '',
    specificationList: [],
    groupon: [],
    issue: [],
    userHasCollect: 0,
    shareImage: "",
    comment: {},
    attribute: [],
    brand: {},
    productList: [],
    info: {},
    isShowTag: false,//控制品牌商标签是否显示
    goodsList: [],//大家都在看
    richtext: '',//富文本
    isShowPopup: false,
    checkedTagValue: '请选择规格数量',
    GuiGe: '',
    Color: '',
    goodsNum: 1,//弹出层中选择的商品数量
    goodsPrice: 0,//弹出层中显示的商品价格
    GuiGeIndex: -1,
    ColorIndex: -1,
    cartNum:-1,
    productId:0,
  },

  // 发送请求获取商品详情
  getGoodsDetailData(id) {
    let that = this;
    util.request(GoodsDetail + '?id=' + id).then(res => {
      if (res.errno == 0) {
        // 判断是否需要渲染品牌商tag
        let flag = true;
        if (Object.keys(res.data.brand).length) {
          flag = true;
        } else {
          flag = false;
        }

        let textrich = res.data.info.detail;

        that.setData({
          specificationList: res.data.specificationList,
          groupon: res.data.groupon,
          issue: res.data.issue,
          userHasCollect: res.data.userHasCollect,
          shareImage: res.data.shareImage,
          comment: res.data.comment,
          attribute: res.data.attribute,
          brand: res.data.brand,
          productList: res.data.productList,
          info: res.data.info,
          isShowTag: flag,
          richtext: res.data.info.detail.replace(/\<img/gi, '<img class="diy-img"'),
        })
      }
    })
  },
  // 获取大家都在看商品数据
  getGoodsRelatedData(id) {
    let that = this;
    util.request(GoodsRelated, {
      id: id
    }).then(res => {
      if (res.errno == 0) {
        that.setData({
          goodsList: res.data.goodsList
        })
      }
    })
  },
  // 获取购物车商品数量
  getGoodsInCartNum(){
    let that = this;
    util.request(getCartData).then(res=>{
      if(res.errno==0){
        that.setData({
          cartNum:res.data.cartTotal.goodsCount
        })
      }
    })
  },

  // 打开弹出框
  showPopup() {
    this.setData({
      isShowPopup: true
    })
  },
  // 关闭popup
  closePopup() {
    this.setData({
      isShowPopup: false
    })
  },


  // 选择品类标签
  checkItem(e) {
    // console.log(e);
    // e.currentTarget.dataset.cate

    if (e.currentTarget.dataset.index == 0) {
      this.data.GuiGe = ''
      this.data.GuiGe = e.currentTarget.dataset.value
      this.setData({
        checkedTagValue: this.data.GuiGe + '          ' + this.data.Color,
        GuiGeIndex: e.currentTarget.dataset.cate + e.currentTarget.dataset.idx
      })
    }
    if (e.currentTarget.dataset.index == 1) {
      this.data.Color = ''
      this.data.Color = e.currentTarget.dataset.value
      this.setData({
        checkedTagValue: this.data.GuiGe + '          ' + this.data.Color,
        ColorIndex: e.currentTarget.dataset.cate + e.currentTarget.dataset.idx
      })
    }

    this.data.productList.forEach(item => {
      if (item.specifications[0] == this.data.GuiGe && item.specifications[1] == this.data.Color) {
        this.setData({
          goodsPrice: item.price,
          productId:item.id
        })
      }
    })

  },


  // 弹出层步进器发生变化
  onChange(e) {
    this.setData({
      goodsNum: e.detail
    })
  },


  // 加入购物车
  putIntoCart(){
    let that = this;
    if(!wx.getStorageSync('token')){
      util.showErrorToast('请先登录账号！')
      setTimeout(()=>{
        wx.navigateTo({
          url: '/pages/login/login?needback=1',
        });
      },1500)
      
      return
    }

    if(this.data.checkedTagValue!='请选择规格数量'){
      util.request(putGoodsIntoCart,{
        goodsId:this.data.goodsId,
        number:this.data.goodsNum,
        productId:this.data.productId
      },'POST').then(res=>{
        if(res.errno==0){
          wx.showToast({
            title: '添加成功~~',
            icon:'success'
          })
          that.setData({
            isShowPopup:false
          })
          this.getGoodsInCartNum();
        }else{
          util.showErrorToast(res.errmsg)
        }
      })


      
    }else{
      util.showErrorToast('请选择商品规格数量')
      that.setData({
        isShowPopup:true
      })
    }
  },


  // 立即购买
  purchaseNow(){

  },
  // 去购物车页面
  toCartPage(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },


  // 生命周期函数--监听页面加载

  onLoad(options) {
    let id = options.id;
    this.setData({
      goodsId: id
    })
    this.getGoodsDetailData(id);
    this.getGoodsRelatedData(id);
    this.getGoodsInCartNum();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 每次进入页面都要获取购物车数据
    this.getGoodsInCartNum();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})